//
//  CodeTableViewCell.m
//  IntelligentHousehold
//
//  Created by Chen on 2017/6/24.
//  Copyright © 2017年 Cassie. All rights reserved.
//

#import "CodeTableViewCell.h"

@interface CodeTableViewCell ()<UITextFieldDelegate>
{
    NSInteger timeOut;//短信失效时间
    
    NSString *codeStr;//验证码
    NSString *telStr;
    NSString *timeStr;//倒计时 分
}

@property (weak, nonatomic) IBOutlet UITextField *tf;
@property (weak, nonatomic) IBOutlet UIButton *btn;
/**  */
@property (nonatomic , strong) NSTimer *timer;
@property (nonatomic, strong) NSTimer *timer1;
/**  */
@property (nonatomic , assign) NSInteger count;
@end

@implementation CodeTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    self.btn.layer.cornerRadius = 15;
    self.btn.clipsToBounds = YES;
    self.btn.layer.borderWidth = 1.5;
    self.btn.enabled = NO;
    // 不可点击样式
    [self.btn setTitle:@"获取验证码" forState:(UIControlStateDisabled)];
    [self.btn setTitleColor:[UIColor lightGrayColor] forState:(UIControlStateDisabled)];
    self.btn.layer.borderColor = [UIColor lightGrayColor].CGColor;
    
    // 点击样式
    [self.btn setTitle:@"获取验证码" forState:(UIControlStateNormal)];
    [self.btn setTitleColor:[UIColor purpleColor] forState:(UIControlStateNormal)];
    
    [self.tf addTarget:self action:@selector(textFChanged) forControlEvents:UIControlEventEditingChanged];
    
}



- (void)setTfString:(NSString *)tfString{
    _tfString = tfString;
    
    if (tfString.length == 11 && self.timer == nil) {
            self.btn.enabled = YES;
            self.btn.layer.borderColor = [UIColor purpleColor].CGColor;
    } else if (self.timer == nil){
        self.btn.enabled = NO;
        self.btn.layer.borderColor = [UIColor lightGrayColor].CGColor;
    }
}



- (IBAction)btnAction:(UIButton *)sender {
    
    if (self.timer == nil) {
        self.count = 120;
        [self.superview endEditing:YES];
        self.btn.enabled = NO;
        self.btn.layer.borderColor = [UIColor lightGrayColor].CGColor;
        self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countAction) userInfo:nil repeats:YES];
    }
    
}

- (void)countAction{
    
    
    [self.btn setTitle:[NSString stringWithFormat:@"%ld秒",self.count] forState:(UIControlStateDisabled)];
    self.count--;
    if (self.count == 0) {
        if (self.tfString.length == 11) {
            self.btn.enabled = YES;
            [self.btn setTitle:@"获取验证码" forState:(UIControlStateNormal)];
            self.btn.layer.borderColor = [UIColor purpleColor].CGColor;
            

        } else {
            self.btn.enabled = NO;
            [self.btn setTitle:@"获取验证码" forState:(UIControlStateDisabled)];
            self.btn.layer.borderColor = [UIColor lightGrayColor].CGColor;
        }
        [self.timer invalidate];
        self.timer = nil;
    }else if (self.count == 119){
        // 获取验证码
        [self getDuanXin];
    }
}

-(void)getDuanXin{
    
    
    
    NSString *url = [NSString stringWithFormat:DUANXIN,RealmName];
    
    NSMutableDictionary *parameDic = [NSMutableDictionary dictionary];
    [parameDic setObject:_tfString forKey:@"tell"];
    
    [HttpRequest postWithURLString:url parameters:parameDic success:^(id responseObject) {
        NSLog(@"%@",responseObject);
        

        timeStr = [responseObject objectForKey:@"time"];
        timeOut = 60*timeStr.integerValue;
        codeStr = [NSString stringWithFormat:@"%@",[responseObject objectForKey:@"rand"]];
        telStr = [NSString stringWithFormat:@"%@",[responseObject objectForKey:@"tell"]];
        
        [self timeOutHandle];
        
        
    } failure:^(NSError *error) {
        
    }];

    
    
}

//验证码有效倒计时
-(void)timeOutHandle{
    self.timer1 = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeOutAction) userInfo:nil repeats:YES];
}

-(void)timeOutAction{
    timeOut--;
    
    
    if (timeOut == 2 && [self.delegate respondsToSelector:@selector(CodeTableViewCellPassTimeOut:CodeStr:InputCodeStr:TelStr:)]) {
        [self.delegate CodeTableViewCellPassTimeOut:0 CodeStr:@"" InputCodeStr:@"" TelStr:@""];
        [self.timer1 invalidate];
        self.timer1 = nil;
        
    }else if (timeOut == 60*timeStr.integerValue-1 && [self.delegate respondsToSelector:@selector(CodeTableViewCellPassTimeOut:CodeStr:InputCodeStr:TelStr:)]){
        
        [self.delegate CodeTableViewCellPassTimeOut:100 CodeStr:codeStr InputCodeStr:@"" TelStr:telStr];
        
    
    }
}
-(void)textFChanged{
    
    if ([self.delegate respondsToSelector:@selector(CodeTableViewCellPassTimeOut:CodeStr:InputCodeStr:TelStr:)]){
        
        [self.delegate CodeTableViewCellPassTimeOut:100 CodeStr:codeStr InputCodeStr:_tf.text TelStr:telStr];
        
        
    }


}








- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
